$(function () {
    layui.config({
        base: './../../../static/lib/dist/'
    });

    layui.use([ 'laydate', 'form', 'notice', 'table', 'selectPlus'],function () {
        var form = layui.form;
        var notice = layui.notice;
        var table = layui.table;
        var laydate = layui.laydate;
        var selectPlus = layui.selectPlus;

        var agencylist = null
        getChildAgencys().then(value => {
            if(value.code == 200){
                //渲染多选
                agencylist = selectPlus.render({
                    el: '#agency',
                    data: value.data,
                    type: "radio",
                    valueName: "agencyCode",
                    label: ["agencyName"],
                    values: layui.data('user').info.agencyCode
                });
            }else{
                notice.error('查询用户机构信息失败')
            }
        })

        function inittable() {
            let initjson = {
                elem: '#userlist',
                url: 'http://127.0.0.1:8098/amc/sys/base/user/page',
                params: {
                    agencyCode: layui.data('user').info.agencyCode,
                    userName: '',
                    cancel: ''
                },
                headers: {'authorization': layui.data('user').token},
                id: 'user_tab',
                cols: [[
                    {type:'checkbox', fixed: 'left'}
                    ,{field:'id', title: 'ID', sort: true}
                    ,{field:'name', title: '用户姓名'}
                    ,{field:'uname', title: '账户名'}
                    ,{field:'agencyCode', title: '所在机构', templet: function (res) {
                            return res.agencyInfo.agencyName
                        }}
                    ,{field:'utype', title: '账户类型', templet: function (res) {
                            if(res.utype == null){
                                return '后台管理账户'
                            }else if(res.utype == 1){
                                return '接口使用账户'
                            }else if(res.utype == 2){
                                return '综合账户'
                            }
                        }}
                    ,{field:'groupInfos', title: '所属部门', templet: function (res) {
                            if(res.groupInfos ==null || res.groupInfos.length<=0){
                                return ''
                            }else{
                                let tt = ''
                                res.groupInfos.forEach(gi=>{
                                    tt+='<span class="layui-btn layui-btn-normal layui-btn-mini">'+gi.groupName+'</span>'
                                })
                                return tt
                            }
                        }}
                    ,{field:'roleInfos', title: '职务', templet: function (res) {
                            if(res.roleInfos ==null || res.roleInfos.length<=0){
                                return ''
                            }else{
                                let tt = ''
                                res.roleInfos.forEach(ur=>{
                                    let at = ur.roleInfo.roleName+'('+ur.groupInfo.groupName+')'
                                    tt+='<span class="layui-btn layui-btn-normal layui-btn-mini">'+at+'</span>'
                                })
                                return tt
                            }
                        }}
                    ,{field:'tel', title: '联系电话'}
                    ,{field:'email', title: '邮箱'}
                    ,{field:'remark', title: '备注'}
                    ,{field:'createTime', title: '创建时间'}
                    ,{field:'createUser', title: '创建人', templet:function (res) {
                            if(res.createUser !=null){
                                return res.createUserInfo.name
                            }else{
                                return ''
                            }
                        }}
                    ,{field:'lastUpdateTime', title: '最近更新时间'}
                    ,{field:'lastUpdateUser', title: '最近更新人', templet:function (res) {
                            if(res.lastUpdateUser !=null){
                                return res.lastUpdateUserInfo.name
                            }else{
                                return ''
                            }
                        }}
                    ,{field:'cancel', title: '状态', templet: function (res) {
                            if(res.cancel){
                                return '<span style="color: #F581B1;">注销</span>'
                            }else{
                                return ''
                            }
                        }}
                    ,{fixed: 'right', title:'操作', toolbar: '#row_bar', width:150}
                ]]
            }
            tableinit(table, initjson)
        }
        inittable()

        function tableReload(){
            var obj = agencylist.getChecked();
            table.reload('user_tab', {
                where: {
                    agencyCode: obj.values[0],
                    userName: $('#userName').val(),
                    cancel: $('#cancel').val()
                } //设定异步数据接口的额外参数
            });
        }

        form.on('submit(search)', function (data) {
            tableReload()
        })

        //监听行工具事件
        table.on('tool(userfilter)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确认要删除吗', function(index){
                    let userIds = []
                    userIds.push(data.id)
                    deleteUsers(userIds).then(value => {
                        if(value.code == 200){
                            layer.msg('删除成功', {icon: 1});
                            tableReload()
                        }else{
                            layer.msg('删除失败，'+value.msg)
                        }
                    }).catch(reason => {
                        layer.msg('服务器未连结')
                    })
                });
            } else if(obj.event === 'cancel'){
                layer.confirm('确认要更改状态吗', function(index){
                    let agencyCodes = []
                    agencyCodes.push(data.agencyCode)
                    cancelUser(data.id, !data.cancel).then(value => {
                        if(value.code == 200){
                            layer.msg('更改成功', {icon: 1});
                            tableReload()
                        }else{
                            layer.msg('更改失败，'+value.msg)
                        }
                    }).catch(reason => {
                        layer.msg('服务器未连结')
                    })
                });
            } else if(obj.event === 'edit'){
                xadmin.open('用户更新','./user-edit.html',800,600)
                layui.data('userinfo', {
                    key: 'edit',
                    value: data
                })
            }else if(obj.event === 'assigngroup'){
                xadmin.open('分配部门','./user_assign_group.html',500,400)
                layui.data('userinfo', {
                    key: 'assign_group',
                    value: data
                })
            }else if(obj.event === 'assignrole'){
                xadmin.open('分配职务','./user_assign_role.html',800,600)
                layui.data('userinfo', {
                    key: 'assign_role',
                    value: data
                })
            }

        });

        $('#delAll').click(function () {
            let checkStatus = table.checkStatus('user_tab');
            let checkdata = checkStatus.data
            if(checkdata.length <= 0){
                notice.warning('请选择要删除的机构')
                return false
            }
            let userIds = []
            checkdata.forEach(cd => {
                userIds.push(cd.id)
            })
            layer.confirm('确认要删除吗？',function(index){
                deleteUsers(userIds).then(value => {
                    if(value.code == 200){
                        layer.msg('删除成功', {icon: 1});
                        tableReload()
                    }else{
                        layer.msg('删除失败，'+value.msg)
                    }
                }).catch(reason => {
                    layer.msg('服务器未连结')
                })

            });
        })
    })
})